
package com.projetinfoent.donnermeteo;

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import javax.xml.namespace.QName;
import javax.xml.transform.sax.SAXSource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlSeeAlso;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.projetinfoent.asyncmeteo.CurrentWeather;
import com.projetinfoent.asyncmeteo.SituationMeteo;
import com.projetinfoent.utils.NamespaceFilter;

/**
 * This class was generated by Apache CXF 2.4.2
 * 2011-10-16T22:23:27.662+02:00
 * Generated source version: 2.4.2
 * 
 */
public final class DonnerMeteoService_DonnerMeteoServiceSOAP_Client implements MessageListener {

	private static final QName SERVICE_NAME = new QName("http://www.projetinfoent.com/donnerMeteo", "donnerMeteoService");
	static URL wsdlURL;

	//Variables JMS
	private ConnectionFactory connectionFactory; 
	private Destination request, response;
	InitialContext jndiContext;
	Connection connection;
	Session session;

	private DonnerMeteoService_DonnerMeteoServiceSOAP_Client() {

		try {
			// Initialise les attributs connectionFactory et destination.
			// Création d'un contexte JNDI
			jndiContext = new InitialContext();
			// Lookup de la fabrique de connexion et de la destination
			connectionFactory = (ConnectionFactory) jndiContext.lookup("connectionFactory"); 
			request = (Destination) jndiContext.lookup("Request");
			response = (Destination) jndiContext.lookup("Response");
			connection = connectionFactory.createConnection();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			// Création d'un MessageConsumer
			MessageConsumer consumer = session.createConsumer(request);
			
			// Reception des messages
			consumer.setMessageListener(this);
			connection.start();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String args[]) throws java.lang.Exception {
		wsdlURL = DonnerMeteoService_Service.WSDL_LOCATION;
		if (args.length > 0 && args[0] != null && !"".equals(args[0])) { 
			File wsdlFile = new File(args[0]);
			try {
				if (wsdlFile.exists()) {
					wsdlURL = wsdlFile.toURI().toURL();
				} else {
					wsdlURL = new URL(args[0]);
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		DonnerMeteoService_DonnerMeteoServiceSOAP_Client main = new DonnerMeteoService_DonnerMeteoServiceSOAP_Client();
		String smes = new String();
		Scanner inputStream = new Scanner(System.in);
		while ( !(smes.compareTo("Q") == 0) ) {
			smes = inputStream.nextLine();
		}
		System.exit(0);
	}

	@Override
	public void onMessage(Message message) {
		TextMessage text = (TextMessage) message;
		com.projetinfoent.asyncmeteo.RequeteMeteo s;
		try {
			if (text instanceof TextMessage) {
				JAXBContext context;
				context = JAXBContext.newInstance("com.projetinfoent.asyncmeteo");
				System.out.println("Message reçu: " + text.getText());
				Unmarshaller u = context.createUnmarshaller();
				s = (com.projetinfoent.asyncmeteo.RequeteMeteo) u.unmarshal(new StringReader(text.getText()));
				
				if (s != null){
					//Le process bpel a lieu ici
					DonnerMeteoService_Service ss = new DonnerMeteoService_Service(wsdlURL, SERVICE_NAME);
					DonnerMeteoService port = ss.getDonnerMeteoServiceSOAP();  
					System.out.println("Invoking donnerMeteo...");
					com.projetinfoent.donnermeteo.RequeteMeteo _donnerMeteo_parameters = new RequeteMeteo() ;
					_donnerMeteo_parameters.setClient(s.getClient());
					_donnerMeteo_parameters.setPays(s.getPays());
					_donnerMeteo_parameters.setVille(s.getVille());
					com.projetinfoent.donnermeteo.SituationMeteoTmp _donnerMeteo__return = port.donnerMeteo(_donnerMeteo_parameters);
					System.out.println("Réponse météo=" + _donnerMeteo__return.getReponseMeteo());
					
					
					//creer un CurrentWeather et le copier en SituationMeteo
					com.projetinfoent.asyncmeteo.CurrentWeather cw;
					JAXBContext cwc = JAXBContext.newInstance(CurrentWeather.class);
					
					//Ajout d'un filtre pour ajouter le namespace
					//Create an XMLReader to use with our filter
					XMLReader reader = XMLReaderFactory.createXMLReader();
					//Create the filter (to add namespace) and set the xmlReader as its parent.
					NamespaceFilter inFilter = new NamespaceFilter("http://www.projetinfoent.com/asyncMeteo/", true);
					inFilter.setParent(reader);
					//Prepare the input, in this case a java.io.File (output)
					InputSource is = new InputSource(new StringReader(_donnerMeteo__return.getReponseMeteo()));
					//Create a SAXSource specifying the filter
					SAXSource source = new SAXSource(inFilter, is);
					//Do unmarshalling
					Unmarshaller ucw = cwc.createUnmarshaller();
					cw =  (com.projetinfoent.asyncmeteo.CurrentWeather) ucw.unmarshal(source);
					
					// Si unmarshalling reussi, creation objet ReponseMeteo et marshall pour envoie
					if (cw != null){
						System.out.println("" + cw.Location);
						com.projetinfoent.asyncmeteo.SituationMeteo sm = new com.projetinfoent.asyncmeteo.SituationMeteo();
						com.projetinfoent.asyncmeteo.SituationMeteo.ReponseMeteo rm = new com.projetinfoent.asyncmeteo.SituationMeteo.ReponseMeteo();
						rm.setMeteo(cw.toMeteo());
						sm.setReponseMeteo(rm);
						com.projetinfoent.asyncmeteo.RequeteMeteo req = new com.projetinfoent.asyncmeteo.RequeteMeteo() ;
						req.setClient(s.getClient());
						req.setPays(s.getPays());
						req.setVille(s.getVille());
						sm.setRequeteMeteo(req);
						//Marshall situation météo + envoie producer
						StringWriter sw = new StringWriter();
						Marshaller marsh = context.createMarshaller();
						marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
						marsh.marshal(sm,sw);
						System.out.println(sw);
						TextMessage message1 = session.createTextMessage(sw.toString());
						MessageProducer producer = session.createProducer(response);
						producer.send(message1);
						connection.start();
					}
					
					
				}



			}
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}



}
